package cn.iocoder.yudao.module.design.controller.admin.relativednc;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.relativednc.vo.*;
import cn.iocoder.yudao.module.design.convert.relativednc.RelativedNcConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.relativednc.RelativedNcDO;
import cn.iocoder.yudao.module.design.service.relativednc.RelativedNcService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 关联人表请求")
@RestController
@RequestMapping("/design/relatived-nc")
@Validated
public class RelativedNcController {

    @Resource
    private RelativedNcService relativedNcService;

    @PostMapping("/create")
    @Operation(summary ="创建关联人表请求")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:create')")
    public CommonResult<Long> createRelativedNc(@Valid @RequestBody RelativedNcCreateReqVO createReqVO) {
        return success(relativedNcService.createRelativedNc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新关联人表请求")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:update')")
    public CommonResult<Boolean> updateRelativedNc(@Valid @RequestBody RelativedNcUpdateReqVO updateReqVO) {
        relativedNcService.updateRelativedNc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除关联人表请求")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:delete')")
    public CommonResult<Boolean> deleteRelativedNc(@RequestParam("id") Long id) {
        relativedNcService.deleteRelativedNc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得关联人表请求")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<RelativedNcRespVO> getRelativedNc(@RequestParam("id") Long id) {
        RelativedNcDO relativedNc = relativedNcService.getRelativedNc(id);
        return success(RelativedNcConvert.INSTANCE.convert(relativedNc));
    }

    @GetMapping("/list")
    @Operation(summary ="获得关联人表请求列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<List<RelativedNcRespVO>> getRelativedNcList(@RequestParam("ids") Collection<Long> ids) {
        List<RelativedNcDO> list = relativedNcService.getRelativedNcList(ids);
        return success(RelativedNcConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得关联人表请求分页")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<PageResult<RelativedNcRespVO>> getRelativedNcPage(@Valid RelativedNcPageReqVO pageVO) {
        PageResult<RelativedNcDO> pageResult = relativedNcService.getRelativedNcPage(pageVO);
        return success(RelativedNcConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出关联人表请求 Excel")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:export')")
   
    public void exportRelativedNcExcel(@Valid RelativedNcExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RelativedNcDO> list = relativedNcService.getRelativedNcList(exportReqVO);
        // 导出 Excel
        List<RelativedNcExcelVO> datas = RelativedNcConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "关联人表请求.xls", "数据", RelativedNcExcelVO.class, datas);
    }
    @GetMapping("/getRelativedNcList")
    @Operation(summary ="获得关联人表请求列表")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<List<Map<String, Object>>> getRelativedNcList() {
        List<Map> list = relativedNcService.getRelativedNcListByUserId();

        List<Map<String, Object>> distinctList = new ArrayList<>();
        Set<Long> userIds = new HashSet<>();

        for (Map<String, Object> map : list) {
            Long userId = Long.valueOf(map.get("userId").toString()) ;
            if (!userIds.contains(userId)) {
                distinctList.add(map);
                userIds.add(userId);
            }
        }
        return success(distinctList);
    }

    @GetMapping("/getRelativedNcReqList")
    @Operation(summary ="获得关联人表请求列表")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<List<Map>> getRelativedNcReqList() {
        List<Map> list = relativedNcService.getRelativedNcReqListByUserId();
        return success(list);
    }

    @PostMapping("/dealRelativedNc")
    @Operation(summary ="处理关联人请求")
    @PreAuthorize("@ss.hasPermission('design:relatived-nc:query')")
    public CommonResult<Boolean> dealRelativedNc(@Valid @RequestBody RelativedNcDealVO vo) {
          relativedNcService.dealRelativedNc(vo);
        return success( true);
    }
}
